<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Frontpage extends CI_Controller {
    private $id;

    public function __construct() {
        parent::__construct();
        $this->check();
        $this->load->helper('url');
        $this->load->model('admin/admin_m');
        $this->load->model('admin/frontpage_m');
    }
    
    public function check(){
        $this->id = $this->session->userdata('user_id');
        if(empty($this->id)){
            redirect('admin/user/login');
        }
    }
    
    public function index(){
        $data['active'] = 3;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Frontpage'=>'frontpage','List'=>'frontpage'));
        $data['frontpage'] = $this->frontpage_m->get_frontpage();
        $data['page'] = 1;
        $data['count'] = $this->frontpage_m->get_frontpage_count();
        $data['start'] = 0;
        $data['content'] = $this->load->view('admin/frontpage/list_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function page($page_number=1){
        $data['active'] = 3;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Frontpage'=>'frontpage','List'=>'frontpage'));
        $data['frontpage'] = $this->frontpage_m->get_frontpage($page_number);
        $data['page'] = $page_number;
        $data['count'] = $this->frontpage_m->get_frontpage_count();
        $data['start'] = 0;
        $data['content'] = $this->load->view('admin/frontpage/list_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function add(){
        $data['active'] = 3;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Frontpage'=>'frontpage','List'=>'frontpage','Add'=>'frontpage/add'));
        $data['content'] = $this->load->view('admin/frontpage/add_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function add_submit(){
        $input['name'] = $this->input->post('name');
        $input['link1'] = $this->input->post('link1');
        $input['link2'] = $this->input->post('link2');
        $input['link3'] = $this->input->post('link3');
        $input['text1'] = $this->input->post('text1');
        $input['text2'] = $this->input->post('text2');
        $input['text3'] = $this->input->post('text3');
        $input['type'] = $this->input->post('type');
        $error = false;
        if ($_FILES["img1"]["error"] > 0) {
            $this->session->set_flashdata('f_frontpage','Photo error. Please try again');
            $error = true;
            redirect('admin/frontpage/add');
        } else if(strlen($_FILES["img1"]["name"]) == 0){
            $this->session->set_flashdata('f_frontpage','Photo error. Please upload a photo');
            $error = true;
            redirect('admin/frontpage/add');
        } else {
            $name = time().preg_replace('/\s+/', '_', $_FILES["img1"]["name"]);
            move_uploaded_file($_FILES["img1"]["tmp_name"], "./photo/frontpage/" . $name);
            $input['img1'] = $name;
        }
        if($input['type']>=1){
            if ($_FILES["img2"]["error"] > 0) {
                $this->session->set_flashdata('f_frontpage','Photo error. Please try again');
                $error = true;
                redirect('admin/frontpage/add');
            } else if(strlen($_FILES["img2"]["name"]) == 0){
                $this->session->set_flashdata('f_frontpage','Photo error. Please upload a photo');
                $error = true;
                redirect('admin/frontpage/add');
            } else {
                $name = time().preg_replace('/\s+/', '_', $_FILES["img2"]["name"]);
                move_uploaded_file($_FILES["img2"]["tmp_name"], "./photo/frontpage/" . $name);
                $input['img2'] = $name;
            }
        }
        if($input['type']==2){
            if ($_FILES["img3"]["error"] > 0) {
                $this->session->set_flashdata('f_frontpage','Photo error. Please try again');
                $error = true;
                redirect('admin/frontpage/add');
            } else if(strlen($_FILES["img3"]["name"]) == 0){
                $this->session->set_flashdata('f_frontpage','Photo error. Please upload a photo');
                $error = true;
                redirect('admin/frontpage/add');
            } else {
                $name = time().preg_replace('/\s+/', '_', $_FILES["img3"]["name"]);
                move_uploaded_file($_FILES["img3"]["tmp_name"], "./photo/frontpage/" . $name);
                $input['img3'] = $name;
            }
        }
        if(!$error){
            $this->frontpage_m->add($input);
            $this->session->set_flashdata('f_frontpage','Frontpage has been added');
            redirect('admin/frontpage');
        }else{
            $this->session->set_flashdata('f_frontpage','There\'s an error while adding frontpage');
            redirect('admin/frontpage');
        }
        
    }
    
    public function edit($id){
        $data['active'] = 3;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Frontpage'=>'frontpage','List'=>'frontpage','Edit'=>'frontpage/edit'));
        $data['frontpage'] = $this->frontpage_m->get_frontpage(0,$id)->first_row();
        $data['content'] = $this->load->view('admin/frontpage/edit_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function edit_submit(){
        $id = $this->input->post('id');
        $input['id'] = $id;
        $input['name'] = $this->input->post('name');
        $input['link1'] = $this->input->post('link1');
        $input['link2'] = $this->input->post('link2');
        $input['link3'] = $this->input->post('link3');
        $input['text1'] = $this->input->post('text1');
        $input['text2'] = $this->input->post('text2');
        $input['text3'] = $this->input->post('text3');
        $input['type'] = $this->input->post('type');
        if(strlen($_FILES["img1"]["name"]) > 0){
            if ($_FILES["img1"]["error"] > 0) {
                $input['img1'] = null;
            } else {
                $name = time() . preg_replace('/\s+/', '_', $_FILES["img1"]["name"]);
                move_uploaded_file($_FILES["img1"]["tmp_name"], "./photo/frontpage/" . $name);
                $input['img1'] = $name;
            }
        }
        if($input['type']>=1){
            if (strlen($_FILES["img2"]["name"]) > 0) {
                if ($_FILES["img2"]["error"] > 0) {
                    $input['img2'] = null;
                } else {
                    $name = time() . preg_replace('/\s+/', '_', $_FILES["img2"]["name"]);
                    move_uploaded_file($_FILES["img2"]["tmp_name"], "./photo/frontpage/" . $name);
                    $input['img2'] = $name;
                }
            }
        }
        if($input['type']==2){
            if (strlen($_FILES["img3"]["name"]) > 0) {
                if ($_FILES["img3"]["error"] > 0) {
                    $input['img3'] = null;
                } else {
                    $name = time() . preg_replace('/\s+/', '_', $_FILES["img3"]["name"]);
                    move_uploaded_file($_FILES["img3"]["tmp_name"], "./photo/frontpage/" . $name);
                    $input['img3'] = $name;
                }
            }
        }
        $this->session->set_flashdata('f_frontpage','Frontpage item has been edited.');
        $this->frontpage_m->edit($input);
        redirect('admin/frontpage/edit/'.$id);
    }
    
    public function delete($id){
        $this->frontpage_m->delete($id);
        $this->session->set_flashdata('f_frontpage','Frontpage has been deleted');
        redirect('admin/frontpage');
    }
}

?>